﻿using System;
using System.Collections.Generic;
using System.Web;

namespace AppBox
{
    public class XMenuHelper
    {
        public static List<MyMenu> GetMyMenuCollection(XMenuCollection oldMenus)
        {
            List<MyMenu> newMenus = new List<MyMenu>();
            ResolveMenuCollection(oldMenus, newMenus, 0, 0);

            return newMenus;
        }

        private static int ResolveMenuCollection(XMenuCollection oldMenus, List<MyMenu> newMenus, int parentId, int level)
        {
            int count = 0;
            foreach (XMenu menu in oldMenus)
            {
                if (menu.ParentMenuId == parentId)
                {
                    count++;

                    MyMenu my = new MyMenu();
                    newMenus.Add(my);
                    my.TreeLevel = level;
                    my.Id = menu.Id;
                    my.ImageUrl = menu.ImageUrl;
                    my.Name = menu.Name;
                    my.NavigateUrl = menu.NavigateUrl;
                    my.ParentId = menu.ParentMenuId;
                    my.Remark = menu.Remark;
                    //my.Show = menu.IsMainMenu;
                    my.SortIndex = menu.SortIndex;


                    level++;
                    int childCount = ResolveMenuCollection(oldMenus, newMenus, menu.Id, level);
                    if (childCount == 0)
                    {
                        my.IsTreeLeaf = true;
                    }
                    level--;
                }
            }

            return count;
        }

    }
}
